<script setup>
import { ref, getCurrentInstance, onMounted } from 'vue'
import Loading from '../../components/loading.vue';
import dayjs from 'dayjs'
import { useRouter } from "vue-router";

const { proxy } = getCurrentInstance()

const infoBg = new URL('../../assets/img/info_bg.jpg', import.meta.url).href
const cloud = new URL('../../assets/img/cloud.png', import.meta.url).href
const logo = new URL('../../assets/img/logo.png', import.meta.url).href
const loading = ref(true)

/**
 * 跳转
 * @param {*} e
 */
 const goto = (e) => {
  if (!e) return
  window.open(e)
}

/** 生产信息 */
const getTraceBackData = async () => {
  try {
    let res = await proxy.$api.call("qin.getTraceBackData", {
      traceBack: traceabilityCode.value
    })
    if (!res.res) return
    content.value = res.news ? res.news.map(v => Object.assign(v, {fold: false})) : []
  } finally {
    loading.value = false
  }
}

const traceabilityCode = ref('')
onMounted(() => {
  traceabilityCode.value = useRouter().currentRoute.value.query.traceabilityCode
  if (!traceabilityCode.value) {
    toast.fail('信息不存在')
    loading.value = false
    return
  }
  getTraceBackData()
})

const content = ref([
  // {
  //   title: '企业新闻',
  //   icon: 'icon-a-workshopproduction',
  //   color: 'green',
  //   fold: false,
  //   content: []
  // },
])

document.title = '企业新闻'
</script>
<template>
  <Loading v-if="loading"></Loading>
  <div class="top" :style="{ backgroundImage: `url(${infoBg})` }">
    <div class="top-cloud">
      <img :src="cloud" alt="" />
    </div>
    <div class="top-logo">
      <img :src="logo" alt="" />
    </div>
  </div>
  <div class="content">
    <div class="content-title">企业新闻</div>
    <!-- <div class="content-subtitle">晓芹干海參：</div> -->
    <div
      class="content-item"
      v-for="(item, index) in content"
      :key="index"
      :class="item.fold ? 'content-item-fold' : ''"
    >
      <div class="title-flex margin-b" :style="{color: item.color}" @click="item.fold = !item.fold">
        <div class="content-item-title">
          <!-- <i class="iconfont" :class="item.icon"></i> -->
          <div>{{ item.group }}</div>
        </div>
        <i class="iconfont icon-xiajiantou- icon-fold" :class="{'icon-fold-reverse': item.fold}"></i>
      </div>
      <template v-for="(node, nodeIndex) in item.data" :key="nodeIndex">
        <div class="news" @click="goto(node.url)">
          <div class="news__image">
            <img :src="node.image" alt="" />
          </div>
          <div class="news__content">
            <div class="title">{{ node.title }}</div>
            <div class="date">{{ dayjs(node.date).format("YYYY-MM-DD") }}</div>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>
<style lang="less" scoped>
@import url(../../assets/css/common.less);

.news {
  display: flex;

  & + .news {
    margin-top: 10px;
  }

  &__image {
    border-radius: 6px;
    width: 25vw;
    height: 25vw;
    overflow: hidden;

    img {
      width: 100%;
      height: 100%;
    }
  }

  &__content {
    flex: 1;
    box-sizing: border-box;
    padding-left: 10px;
    font-weight: 600;
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    & > .title {
      
    }
    & > .date {
      color: var(--font-hc);
      font-size: 14px;
    }
  }
}
</style>
